﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using Water.Resources.Globalize;

namespace Water.Architecture2.EntityXML
{
    public class EntityFactory
    {
        public static void Create(string path,string entityXmFileName)
        {
            string entityXmlPath = path + entityXmFileName;
            if (File.Exists(entityXmlPath))
            {
                 XmlReaderSettings setting = new XmlReaderSettings();
                 try
                 {
                     setting.IgnoreComments = true;
                     setting.IgnoreWhitespace = true;
                     setting.IgnoreProcessingInstructions = true;
                     using (XmlReader xmlReader = XmlReader.Create(entityXmlPath, setting))
                     {
                         while (xmlReader.Read())
                         {
                             XmlNodeType noteType = xmlReader.NodeType;
                             switch (noteType)
                             { 
                                 case XmlNodeType.Element:
                                     String elementName = xmlReader.Name.ToLower();
                                     if (elementName == "entity-factory")
                                     {
                                         for (int i = 0; i < xmlReader.AttributeCount; i++)
                                         {
                                             xmlReader.MoveToAttribute(i);
                                             string _name = xmlReader.Name.ToLower();
                                             string _path = xmlReader.Value;
                                             if (_name == "show-sql" && !string.IsNullOrEmpty(_path))
                                             {
                                                 Water.DA.DatabaseGlobalSetting.ShowSql=Boolean.Parse(xmlReader.Value);
                                             }
                                         }
                                     }
                                     else if (elementName == "mapping")
                                     {
                                         for (int i = 0; i < xmlReader.AttributeCount; i++)
                                         {
                                             xmlReader.MoveToAttribute(i);
                                             string _name = xmlReader.Name.ToLower();
                                             string _path = xmlReader.Value;
                                             if (_name == "resource" && !string.IsNullOrEmpty(_path))
                                             {
                                                 EntityXMLParser entityXMLParser = new EntityXMLParser();
                                                 path = path.EndsWith("\\") ? path : path + "\\";
                                                 entityXMLParser.Parse(path+_path);
                                             }
                                         }
                                     }
                                     break;
                             }
                         }
                     }
                 } catch (XmlException xe)
                 {
                     throw xe;
                 }
            }
            else
                throw new Exception(string.Format(ExceptionResource.ExceptionFileNotFound, entityXmlPath));
        }
    }
}
